package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.meterService.dto.MsRepairDto;
import com.koron.css2.meterService.mapper.MsRepairMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 校表查询
 * @author 周利斌
 *
 */
public class MsRepairQuery implements ServerInterface{

	private static Logger log = LogManager.getLogger(MsRepairQuery.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<MsRepairDto> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", MsRepairDto.class);
		try {
			String id = JsonUtils.objectToPojo(req.getData(), String.class);
			if(StringUtils.isBlank(id)) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("id不能为空");
				return info;
			}
			MsRepairMapper mapper = factory.getMapper(MsRepairMapper.class);
			MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
			MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
			MsRepairDto msRepair = mapper.selectMsRepair(id);
			Map<String, String> sources = Tools.mapDicByCode(factory, "PIS");  //信息来源
			Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF");  //水表形态
			Map<String, String> repairTypes = Tools.mapDicByCode(factory, "REPAIR_TYPE");  //维检类型
			Map<String, String> calibrationResults = Tools.mapDicByCode(factory, "CALI_RESULT"); //检定结果
			 MeterModelMapper modelmapper = factory.getMapper(MeterModelMapper.class);
            MeterModelBean query = modelmapper.selectMeterModelById(msRepair.getMeterModel());
            msRepair.setMeterModel(query==null?msRepair.getMeterModel():query.getModelName());
            //水表厂商
            MeterFactoryBean factoryBean =  meterFactoryMapper.selectMeterFactoryById(msRepair.getMeterFactory());   
            msRepair.setMeterFactory(factoryBean==null?msRepair.getMeterFactory():factoryBean.getFactoryName());
            //水表类型
            msRepair.setMeterType(Tools.getDicNameByCodeAndValue(factory,"MMT",msRepair.getMeterType()));
            //口径
            MeterBoreBean boreBean = meterBoreMapper.selectMeterBoreById(msRepair.getMeterBore());
            msRepair.setMeterBore(boreBean==null?msRepair.getMeterBore():boreBean.getBoreName());
			if(msRepair.getSourceFlag() != null) {
				msRepair.setSourceFlagName(sources.get(msRepair.getSourceFlag().toString()));
			}
			if(StringUtils.isNotBlank(msRepair.getMeterForm())) {
				msRepair.setMeterForm(meterForms.get(msRepair.getMeterForm()));
			}
			if(StringUtils.isNotBlank(msRepair.getRepairType())) {
				msRepair.setRepairTypeName(repairTypes.get(msRepair.getRepairType()));
			}
			if(msRepair.getCalibrationResult() != null) {
				msRepair.setCalibrationResultName(calibrationResults.get(msRepair.getCalibrationResult() + ""));
			}
			
			msRepair.setProcessStateName(Tools.transProcessStatus(msRepair.getProcessState()));
			info.setData(msRepair);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
